package com.ptmall.app.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpFragment;
import com.ptmall.app.bean.FyBean;
import com.ptmall.app.bean.LogBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.FyAdapter;
import com.ptmall.app.ui.adapter.OrderNoticeAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.xfly.app.ui.dialog.FfDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FyFragment extends BaseMvpFragment {

    PullableListView mListview;
    PullToRefreshLayout mRefreshView;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    FyAdapter mFyAdapter;
    View view;
    int page = 1;
    List<FyBean> mDataLists = new ArrayList<>();
    FfDialog ffDialog;
    String type;

    public static FyFragment newInstance(String type) {
        FyFragment fragment = new FyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_list,container,false);
        return view;

    }

    @Override
    public void initViews() {
        type = getArguments().getString("type");
        mRefreshView = (PullToRefreshLayout) view.findViewById(R.id.refresh_view);
        mListview = (PullableListView) view.findViewById(R.id.listview);
        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;

                getlist();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page ++;
                getlist();

            }
        });
    }

    @Override
    public void initEvents() {

        mFyAdapter = new FyAdapter(getContext(),null);
        mListview.setAdapter(mFyAdapter);
        mFyAdapter.setType(type);
        mFyAdapter.setClickInterface(new FyAdapter.ClickInterface() {
            @Override
            public void btn1Click(FyBean fyBean) {
                ffDialog = new FfDialog(getContext());
                ffDialog.show();
                ffDialog.setonclick(new FfDialog.Onclick() {
                    @Override
                    public void Yes() {
                        Map<String,String> map = new HashMap();
                        map.put("member_id",fyBean.id);
                        apiDataRepository.ffyj(map, new ApiNetResponse<Object>() {
                            @Override
                            public void onSuccess(Object data) {
                                showMsg("发放成功");
                                page = 1;
                                getlist();
                            }
                        });
                    }
                });
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getlist();
    }

    private void getlist(){
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        if(type.equals("1")){
            apiDataRepository.getFyWaitList(map, new ApiNetResponse<List<FyBean>>() {
                @Override
                public void onSuccess(List<FyBean> data) {
                    if (null != mRefreshView) {
                        mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                        mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                    }
                    if (page == 1) {
                        mDataLists.clear();
                        if (null != data && data.size() > 0) {
                            mDataLists.addAll(data);
                        } else {
                        }
                    } else {
                        if (null != data && data.size() > 0) {
                            mDataLists.addAll(data);
                        } else {
                            showMsg(R.string.has_no_more);
                        }
                    }
                    mFyAdapter.pushData(mDataLists);
                }
            });
        }else {
            apiDataRepository.getFyList(map, new ApiNetResponse<List<FyBean>>() {
                @Override
                public void onSuccess(List<FyBean> data) {
                    if (null != mRefreshView) {
                        mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                        mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                    }
                    if (page == 1) {
                        mDataLists.clear();
                        if (null != data && data.size() > 0) {
                            mDataLists.addAll(data);
                        } else {
                        }
                    } else {
                        if (null != data && data.size() > 0) {
                            mDataLists.addAll(data);
                        } else {
                            showMsg(R.string.has_no_more);
                        }
                    }
                    mFyAdapter.pushData(mDataLists);
                }
            });
        }

    }
}
